package com.book.service.impl;

import com.book.entity.Users;
import com.book.mapper.UserMapper;
import com.book.service.AuthService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author zzw2000
 * @Date 2022年08月25日 11:32
 * @Description 描述
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Resource
    private UserMapper userMapper;

    /**
     * 用户注册
     *
     * @param username
     * @param password
     * @param sex
     * @param grade
     * @return
     */
    @Transactional
    @Override
    public boolean register(String username, String password, String sex, int grade) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String pwd = encoder.encode(password);
        Users users = new Users(0, username, pwd, "user");
        System.out.println(Objects.equals(encoder.encode(password), pwd));
        if (userMapper.addUsers(users) < 1) {
            throw new RuntimeException("用户注册失败！");
        }
        if (userMapper.addStu(users.getId(), username, sex, grade) < 1) {
            throw new RuntimeException("学生注册失败！");
        }
        return true;
    }
}
